	

	/**
	 * 页面onload事件
	 */
	$(document).ready(function() {
		guaHaoLeiBie();
		yiBaoLeiBie();
		var contentMainState = true;
		$("select").select2({
			language: "zh-CN"
		});
		debugger;
		$(".set_container").find('input').iCheck({
		    checkboxClass: 'icheckbox_square-blue',
		    radioClass: 'iradio_square-blue',
//		    increaseArea: '20%' // optional
		  });
		paramsInit();
		qiYongYibaoChange();
	});
	function qiYongYibaoChange(){
		$("#p_100_105002_420").on("ifChecked",function(){
			accordP100105002420(true);
		})
		$("#p_100_105002_420").on("ifUnchecked",function(){
			accordP100105002420();
		})
	}
	function accordP100105002420(ifcheck){
		if(ifcheck){
			$("#p_9999_9999_114").iCheck('uncheck').iCheck('enable');
			$("#p_100_105002_421").val(0).trigger("change").removeAttr('disabled',true);
			
		}else{
			
			$("#p_9999_9999_114").iCheck('uncheck').iCheck('disable');
			$("#p_100_105002_421").val(0).trigger("change").attr('disabled',true);
		}
	}
	function guaHaoLeiBie(){
		$.get(rootUrl + "/clidoctor/selectDataByScriptId",
				{
				ID : 1878,
				paramStr : '{CurrTime:"' + getcurrentDate() + '",DateNum:"' + getWeek() + '",ltime:"' + gettime() + '",Date:"' + getcurrDate() + '"}'
				}, function(json)
				{
					if (json.result == "Success")
					{
						$("#p_100_105002_418").find("option").remove();
						var option1= $("<option>").val(0).text('请选择');
						$("#p_100_105002_418").append(option1);
						for (var i = 0; i < json.rows.length; i++) {
							var option = $("<option>").val(json.rows[i].bcb01).text(
									json.rows[i].bcb03);
							$("#p_100_105002_418").append(option);
						}
			}
		});
	}
	function yiBaoLeiBie(){
		$.get(rootUrl + "/clidoctor/selectDataByScriptId",
				{
			ID : 10000026,
			paramStr : '{}'
				}, function(json)
				{
					debugger;
					if (json.result == "Success")
					{
						$("#p_100_105002_421").find("option").remove();
						var option1= $("<option>").val(0).text('请选择');
						$("#p_100_105002_421").append(option1);
						for (var i = 0; i < json.rows.length; i++) {
							var option = $("<option>").val(json.rows[i].iaa01).text(
									json.rows[i].iaa03);
							$("#p_100_105002_421").append(option);
						}
					}
				});
	}
	/**
	 * 初始化参数
	 */
	function paramsInit(){
		debugger;
		var url = rootUrl + "/patientsearch/findYaofang";
		$.when(
		$.get(url,{bau01:"('西药房')",corporationid:yiyuanId},function(data){
			var list = data.list;
			debugger;
			for(var i = 0;i<list.length;i++)
			{
				var option1 = $("<option>").attr({value:list[i]["bck01"]}).text(list[i]["bck03"]);
				$("#p_100_105002_108").append(option1);
			}
		}),
		$.get(url,{bau01:"('成药房')",corporationid:yiyuanId},function(data){
			var list = data.list;
			for(var i = 0;i<list.length;i++)
			{
				var option1 = $("<option>").attr({value:list[i]["bck01"]}).text(list[i]["bck03"]);
				$("#p_100_105002_109").append(option1);
			}
		}),
		$.get(url,{bau01:"('中药房')",corporationid:yiyuanId},function(data){
			var list = data.list;
			$("#p_100_105002_110").empty();
			for(var i = 0;i<list.length;i++)
			{
				var option = $("<option>").attr({value:list[i]["bck01"]}).text(list[i]["bck03"]);
				$("#p_100_105002_110").append(option);
			}
		}),
		$.get(url,{bau01:"('发料','卫材库','材料库')",corporationid:yiyuanId},function(data){
			var list = data.list;
//			$("#p_100_105002_404").append($("<option>").attr({value:bck01}).text('当前科室'));
			$("#p_100_105002_404").empty();
			for(var i = 0;i<list.length;i++)
			{
				var option = $("<option>").attr({value:list[i]["bck01"]}).text(list[i]["bck03"]);
				$("#p_100_105002_404").append(option);
			}
		}),
		$.get(rootUrl+"/clidoctor/queryFileInfoByType",{},function(data)
//		$.get(dzbl_cloud+"/1baseInterface/queryFileInfoByType?type=1&jsessionids="+$.cookie("jsessionids"),{},function(data)
				{
					data = data.list;
			        var html = "";
					for (var i = 0;i<data.length;i++)
					{
						html += "<option value='"+data[i].id+"'>"+data[i].name+"</option>"; 
					}
					$("#p_100_105002_390").append(html);
					$("#p_100_105002_390").select2();
					$("#p_100_105002_391").append(html);
					$("#p_100_105002_391").select2();
				})
		).done(
		$.ajax({
			url : rootUrl + "/patientsearch/findSysparams",
			data : {userid:$.cookie('userId'),bck01:bck01},
			success : function(data) {
				debugger;
				var list = data.list;
				var valueArr = {};
				for(var i = 0;i<list.length;i++){
					var cur = list[i]["para_key"];
					if($("#"+cur).length > 0 ){
						var cur_node = $("#"+cur)[0].nodeName ;
						if(cur_node == "SELECT"){
							if(!!list[i].value){
								$("#"+cur).val(list[i].value).trigger("change");
							}
							
						}else if(cur_node == "INPUT"){
							if($("#"+cur).attr("type") == "checkbox"){
//								valueArr[list[i]["para_key"]] = list[i]["value"];
								if(list[i]["value"]=="1")
								{
									$("#"+list[i]["para_key"]).parent().iCheck('check');
								}
								$("#"+list[i]["para_key"]).attr("defvalue", list[i]["defvalue"]);
							}else{
								$("#"+cur).val(list[i].value)
								
							}
						}
					}
				}
				if(!$("#p_9999_9999_22").val())
				{
					$("#p_9999_9999_22").val('1').trigger("change");
				}
				if(!$("#p_100_105002_390").val())
				{
					$("#p_100_105002_390").val('0').trigger("change");
				}
				if(!$("#p_100_105002_391").val())
				{
					$("#p_100_105002_391").val('0').trigger("change");
				}
				//设置医保用药限制联动
				debugger;
				if(!$("#p_100_105002_420").prop("checked")){
					accordP100105002420();
				}
				
			}
		})
		);
	}
	function YiBaoJiaoyan(){
		var flag = true;
		if($("#p_100_105002_420").prop("checked")){
			var lebie = $("#p_100_105002_421").val();
			if(lebie ==0){
				flag = false;
			}
		}
		return flag;
	}
	/*
	 * 参数批量修改
	 */
	function saveParams()
	{
		debugger;
		layer.msg('保存中....', {icon: 16,shade: [0.1, '#393D49'],time:0});
		//校验医保用药限制选中项
		var flag = YiBaoJiaoyan();
		if(!flag){
			layer.alert('启用医保限制用药，请选择医保类别', {icon: 0});
			return false;
		}
		var resultArr105002_1 = [];
		var resultArr105002_0 = [];
		var resultArr9999_1 = [];
		var resultArr9999_0 = [];
		
		var resultArrValue = [];
		var resultArrValue = [];
		var allArr = [];
		
		configArr(resultArr105002_1,resultArr105002_0,resultArr9999_1,resultArr9999_0,resultArrValue);
		for(var i = 0;i<resultArr105002_1.length;i++)
		{
			var temp = resultArr105002_1[i];
			if(!temp.privilege)
			{
				temp.privilege = 0;
			}
		}
		for(var i = 0;i<resultArr105002_0.length;i++)
		{
			var temp = resultArr105002_0[i];
			if(!temp.privilege)
			{
				temp.privilege = 0;
			}
		}
		for(var i = 0;i<resultArr9999_1.length;i++)
		{
			var temp = resultArr9999_1[i];
			if(!temp.privilege)
			{
				temp.privilege = 0;
			}
		}
		for(var i = 0;i<resultArr9999_0.length;i++)
		{
			var temp = resultArr9999_0[i];
			if(!temp.privilege)
			{
				temp.privilege = 0;
			}
		}
		for(var i = 0;i<resultArrValue.length;i++)
		{
			var temp = resultArrValue[i];
			if(!temp.privilege)
			{
				temp.privilege = 0;
			}
		}
		var url = rootUrl + "/patientsearch/updateAllSysparams";
		var params = {};
		params.resultArr105002_1 = JSON.stringify(resultArr105002_1);
		params.resultArr105002_0 = JSON.stringify(resultArr105002_0);
		params.resultArr9999_1 = JSON.stringify(resultArr9999_1);
		params.resultArr9999_0 = JSON.stringify(resultArr9999_0);
		params.resultArrValue = JSON.stringify(resultArrValue);
		debugger;
		allArr = $.merge(allArr,resultArr105002_1);
		allArr = $.merge(allArr,resultArr105002_0);
		allArr = $.merge(allArr,resultArr9999_1);
		allArr = $.merge(allArr,resultArr9999_0);
		allArr = $.merge(allArr,resultArrValue);
		//*************************************************************************************************************************************privilege配置
//		var privilege1 = [106,107,243,351,389,392,386,393,100,246,394,401,402,418];//个人
//		var privilege3 = [108,109,110,404];//科室
		//设置参数授权（个人、公共、科室）
		console.table(allArr);
		params.allArr = JSON.stringify(allArr);
		params.userid = userid;//用户id
		debugger;
		params.bck01 = bck01;//科室id
		$.post(url,params,function(data){
			if(data.result=="Success")
			{
				layer.msg("保存成功！",{ time: 2000,icon: 1,skin: 'layui-layer-msg1'});
			}
			else
			{
				layer.msg("数据异常，请联系管理员！",{ time: 2000,icon: 2});
			}
		});
	}
	
	function getEleValue($ele_all,resultArr105002_1,resultArr105002_0,resultArr9999_1,resultArr9999_0,resultArrValue,privilege){
		$ele_all.each(function(i,ele){
			var en = ele.nodeName;
			var ids = $(ele).attr("id").split("_"); //["p", "100", "105002", "106"]
			var custom = ids[2];  //判断是那个数组
			if(ele.type == "checkbox" ){  //checkbox 
				 if(custom == "105002"){
					 if($(ele).prop("checked"))
					 {
						 var result = {};
						 result.productid = parseInt(ids[1]);
						 result.programid = parseInt(ids[2]);
						 result.paramno = parseInt(ids[3]);
						 result.privilege = privilege;
						 result.value = 1;
						 resultArr105002_1.push(result);
					 }
					 else
					 {
						 var result = {};
						 result.productid = parseInt(ids[1]);
						 result.programid = parseInt(ids[2]);
						 result.paramno =  parseInt(ids[3]);
						 result.privilege = privilege;
						 result.value = 0;
						 resultArr105002_0.push(result);
					 }
					 
				 }else{
					 if($(ele).prop("checked"))
					 {
						 var result = {};
						 result.productid = parseInt(ids[1]);
						 result.programid = parseInt(ids[2]);
						 result.paramno = parseInt(ids[3]);
						 result.privilege = 1;
						 result.value = privilege;
						 resultArr9999_1.push(result);
					 }
					 else
					 {
						 var result = {};
						 result.productid = parseInt(ids[1]);
						 result.programid = parseInt(ids[2]);
						 result.paramno =  parseInt(ids[3]);
						 result.privilege = privilege;
						 result.value = 0;
						 resultArr9999_0.push(result);
					 }
				 }
			}
			else{   // input\select
				var temp1 = {};
				temp1.productid = parseInt(ids[1]);
				temp1.programid = parseInt(ids[2]);
				temp1.paramno = parseInt(ids[3]);
				if(ids[4]=="default"){
					temp1.privilege = 4;
				}else{
					temp1.privilege = privilege;
				}
				temp1.value=$(ele).val()?$(ele).val():"";
				resultArrValue.push(temp1);
			}
		})
		return resultArr105002_1,resultArr105002_0,resultArr9999_1,resultArr9999_0,resultArrValue;
	}
	function configArr(resultArr105002_1,resultArr105002_0,resultArr9999_1,resultArr9999_0,resultArrValue)
	{
		//第一个
		//privilege  (1 个人，2公共，3 科室)
		if($("#set_container").length>0){
			
			var $first_all = $("#personal-params").find("[id^='p_']");
			var privilege = 1;
			getEleValue($first_all,resultArr105002_1,resultArr105002_0,resultArr9999_1,resultArr9999_0,resultArrValue,privilege);
		}
		//第二个
		else if($("#set_container2").length>0){
			var $second_k = $("#privilege_3").find("[id^='p_']");
			var privilege = 3; //公共
			getEleValue($second_k,resultArr105002_1,resultArr105002_0,resultArr9999_1,resultArr9999_0,resultArrValue,privilege);
			var $second_other = $("#public-params .privilege_2").find("[id^='p_']");
			var privilege = 2; //科室
			getEleValue($second_other,resultArr105002_1,resultArr105002_0,resultArr9999_1,resultArr9999_0,resultArrValue,privilege);
		}//第三个
		else if($("#set_container3").length>0){
			var $third_all = $("#global-params").find("[id^='p_']");
			getEleValue($third_all,resultArr105002_1,resultArr105002_0,resultArr9999_1,resultArr9999_0,resultArrValue);
		}	
	}
	/*主题事件change事件*/
	function zhutiChangeEvent(){
		var flag = $("#p_100_105002_392").val();
		if(flag=='one'){
			$("#p_100_105002_386").parents("li").show();
			$("#p_100_105002_393").parents("li").hide();
		}else if(flag=='two'){
			$("#p_100_105002_386").parents("li").hide();
			$("#p_100_105002_393").parents("li").show();
		}
	}
	
	//参数还原
	function resetParams(){
		//
		//询问框
		layer.confirm('确定还原参数为系统初始化值？', {
		  btn: ['确定','取消'], //按钮
		  icon:3
		}, function(){
			cancelParams();
		}, function(){
		});
		
	}
	
	function cancelParams()
	{
		var cancelArr_user = [];
		var cancelArr_public = [];
		configCancelParams(cancelArr_user,cancelArr_public);
		$.ajax({
			url:rootUrl + "/patientsearch/updateUserParams_cancel",
			type:"post",
			dataType:"json",
			data:{"cancelArr_user":JSON.stringify(cancelArr_user),"cancelArr_public":JSON.stringify(cancelArr_public),"userid":$.cookie('userId')},
			success:function(data)
			{
				if (data.result=="Success")
				{
					paramsInit();
					layer.msg('参数还原成功',
					{
					time : 3000,
					icon : 1,
					skin : 'layui-layer-msg1'
					});
				}  
				else
				{
					layer.msg('参数还原失败',{time : 3000,icon : 2,skin : 'layui-layer-msg2'});
				}
			}
		});
	}
	
	function configCancelParams(cancelArr_user,cancelArr_public)
	{
		for ( var obj in valueArr)
		{
			var keyArr = obj.split("_");
			
			var newObj = {};
			if ("106-107-243-351-389-392-386-393-100-246-394".indexOf(keyArr[3])>=0)
			{
				var newKey = keyArr[1] + keyArr[2] + keyArr[3];//sys_userparameters的paramid
				var newValue = $("#" + obj).attr("defvalue");
				newObj.paramid = newKey;
				newObj.value = newValue;
				cancelArr_user.push(newObj);
			}
			else
			{
				newObj.productid = keyArr[1];
				newObj.programeid = keyArr[2];
				newObj.paramno = keyArr[3];
				cancelArr_public.push(newObj);
			}
		}
	}
	
	